﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using SCRUM.Core.Configuration;

namespace SCRUM.Core.BusinessObjects
{
    /// <summary>
    /// Business object which holds following information:
    ///  * ID - object's unique identifier in persistent storage (among objects of same type)
    ///  * SystemID - object's unique identifier in scrum methodology system (derived from ID)
    /// </summary>
    [Serializable()]
    public class BusinessObject
    {
        /// <summary>
        /// Gets or sets the object's ID = unique identifier (among objects of same type)
        /// </summary>
        /// <value>The ID.</value>
        public int ID
        {
            get;
            set;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BusinessObject"/> class.
        /// </summary>
        /// <param name="id">The id.</param>
        protected BusinessObject(int id)
        {
            this.ID = id;
        }

        /// <summary>
        /// Gets or sets the system ID - object's unique identifier in scrum methodology system (derived from ID)
        /// </summary>
        /// <value>The system ID.</value>
        public virtual string SystemID 
        { 
            get
            {
                return this.ID.ToString();
            } 
            set{} 
        }

        /// <summary>
        /// Localizes the string.
        /// </summary>
        /// <param name="resourceKey">The resource key.</param>
        /// <returns></returns>
        protected string LocalizeString(string resourceName, string resourceKey)
        {
            string ret = (string)HttpContext.GetGlobalResourceObject(resourceName, resourceKey);
            
            if(String.IsNullOrEmpty(ret))
            {
                ret = WarnAboutNotLocalizedString 
                    ?String.Format("WARNING : {0}/{1} IS NOT LOCALIZED", resourceName, resourceKey)
                    :resourceKey;
            }

            return ret;
        }

        /// <summary>
        /// This specifies whether to warn about not localized strings or not
        /// </summary>
        public const bool WarnAboutNotLocalizedString = false;

        /// <summary>
        /// Shortens the text to predefined number of chars
        /// </summary>
        /// <param name="text">The text.</param>
        /// <returns></returns>
        public static string ShortenText(string text)
        {
            if (text.Length > ConfigManager.CurrentConfig.OutputProperties.DescriptionMaxLength)
            {
                return (text.Substring(0, ConfigManager.CurrentConfig.OutputProperties.DescriptionMaxLength)) + "...";
            }
            else
            {
                return (text);
            }
        }

        /// <summary>
        /// Makes the system ID from persistent storage ID
        /// </summary>
        /// <param name="prefix">The prefix.</param>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public static string MakeSystemID(string prefix, int id)
        {
            return String.Format("{0}{1}",prefix, id);
        }
    }
}
